//
//  XHFirstCell.m
//  elxc
//
//  Created by fengyn on 16/3/14.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHFirstCell.h"
#import "Services.h"

@implementation XHButtonView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit{
    self.layer.masksToBounds=YES;
    self.layer.cornerRadius=5;
    self.layer.borderColor=[ColorManager colorWithMain].CGColor;
    self.layer.borderWidth=1;
    
    UIButton *button=[[UIButton alloc] init];
    [button setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[ColorManager colorWithMain] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    self.button=button;
    UILabel *label1=[[UILabel alloc] init];
    label1.font=[UIFont boldSystemFontOfSize:17];
    label1.textColor=[ColorManager colorWithMain];
    label1.textAlignment=NSTextAlignmentCenter;
    [self addSubview:label1];
    self.titleLabel=label1;
    UILabel *label2=[[UILabel alloc] init];
    label2.font=[UIFont systemFontOfSize:12];
    label2.textColor=[ColorManager colorWithMain];
    label2.textAlignment=NSTextAlignmentCenter;
    [self addSubview:label2];
    self.detailLabel=label2;
    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shouye_icon_cuxiao"]];
    [self addSubview:imageView];
    self.tagImageView=imageView;
}

- (void)setIsCanSelected:(BOOL)isCanSelected{
    _isCanSelected=isCanSelected;
    if (isCanSelected) {
        self.layer.borderWidth=1;
    }else{
        self.layer.borderWidth=0;
        [self.borderlayer removeFromSuperlayer];
        self.borderlayer=[self setDashBorderWidth:1 cornerRadius:5 andColor:[ColorManager colorWithMain] lineDashPattern:@[@5,@5]];
    }
}

- (void)buttonAction{
    if (self.isCanSelected) {
        [self setSelected:![self isSelected]];
    }
    if ([self.delegate respondsToSelector:@selector(buttonViewDidSelectedAction:)]) {
        [self.delegate buttonViewDidSelectedAction:self];
    }
}

- (void)setSelected:(BOOL)selected{
    self.button.selected=selected;
    if (selected) {
        self.titleLabel.textColor=[UIColor whiteColor];
        self.detailLabel.textColor=[UIColor whiteColor];
    }else{
        self.titleLabel.textColor=[ColorManager colorWithMain];
        self.detailLabel.textColor=[ColorManager colorWithMain];
    }
}

- (BOOL)isSelected{
    return self.button.isSelected;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self updateRects];
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    [self updateRects];
}

- (void)updateRects {
    self.button.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.titleLabel.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height*(57/70.0f));
    if (self.frame.size.height<40) {
        self.titleLabel.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }
    self.detailLabel.frame=CGRectMake(0, 45, self.frame.size.width, 15);
    CGSize imageSize=self.tagImageView.image.size;
    self.tagImageView.frame=CGRectMake(self.frame.size.width-imageSize.width, 0, imageSize.width, imageSize.height);
}

@end

@implementation XHFirstCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.ui_helpButton setRightImage];
    [self.ui_helpButton setHidden:YES];
    self.rowHeight=70;
}

- (void)setdata:(NSArray *)productArray{
    self.m_dataArray=productArray;
    for (UIView *view in self.ui_borderView.subviews) {
        [view removeFromSuperview];
    }
    for(int i=0; i<productArray.count; i++){
        int row=i/3;
        int column=i%3;
        int screenWidth=[UIScreen mainScreen].bounds.size.width;
        int viewWidth=(screenWidth-40-10*(3-1))/3;
        XHButtonView *buttonView=[[XHButtonView alloc] initWithFrame:CGRectMake(column*viewWidth+column*10, row*(self.rowHeight)+row*10, viewWidth, self.rowHeight)];
        buttonView.tag=i;
        buttonView.delegate=self;
        NSDictionary * dic=productArray[i];
        if ([productArray[i] isKindOfClass:[NSString class]]) {
            buttonView.titleLabel.text=[NSString stringWithFormat:@"%@件装",productArray[i]];
            buttonView.detailLabel.text=@"";
            buttonView.tagImageView.hidden=YES;
        }else
        if([dic[@"productType"] isEqualToString:kProductTypeFlow])
        {
            //流量直充
            NSInteger flowCount=[dic[@"productValue"] integerValue];
            if (flowCount<1000) {
                buttonView.titleLabel.text=[NSString stringWithFormat:@"%@M",dic[@"productValue"]];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                buttonView.titleLabel.text=[NSString stringWithFormat:@"%@G",[formatter stringFromNumber:@(gcount)]];
            }
            if ([dic[@"isPromotion"] isEqualToString:@"Y"]) {
                buttonView.detailLabel.text=[NSString stringWithFormat:@"售价:%.2f元",[dic[@"promotionPrice"] doubleValue]];
                buttonView.tagImageView.hidden=NO;
            }else{
                buttonView.detailLabel.text=[NSString stringWithFormat:@"售价:%.2f元",[dic[@"salesPrice"] doubleValue]];
                buttonView.tagImageView.hidden=YES;
            }
            if ([dic[@"stock"] intValue]>0) {
                [buttonView setIsCanSelected:YES];
            }else{
                [buttonView setIsCanSelected:NO];
            }
        }else if([dic[@"productType"] isEqualToString:kProductTypeFlowPackage]){
            //流量屯包
            buttonView.titleLabel.font = [UIFont systemFontOfSize:14];
            NSInteger flowCount=[dic[@"productValue"] integerValue];
            if (flowCount<1000) {
                buttonView.titleLabel.text=[NSString stringWithFormat:@"%@M(%@折)",dic[@"productValue"],dic[@"packageDiscount"]];
            }else{
                float gcount=flowCount/(float)1000;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.numberStyle = NSNumberFormatterRoundFloor;
                buttonView.titleLabel.text=[NSString stringWithFormat:@"%@G(%@折)",[formatter stringFromNumber:@(gcount)],dic[@"packageDiscount"]];
            }
            buttonView.detailLabel.text=[NSString stringWithFormat:@"市场价:%.2f元",[dic[@"salesPrice"] doubleValue]];
            buttonView.tagImageView.hidden=YES;
            if ([dic[@"stock"] intValue]>0) {
                [buttonView setIsCanSelected:YES];
            }else{
                [buttonView setIsCanSelected:NO];
            }
        }else
        {
            buttonView.titleLabel.text=[NSString stringWithFormat:@"%@元",dic[@"productValue"]];
            if ([dic[@"isPromotion"] isEqualToString:@"Y"]) {
                buttonView.detailLabel.text=[NSString stringWithFormat:@"售价:%.2f元",[dic[@"promotionPrice"] doubleValue]];
                buttonView.tagImageView.hidden=NO;
            }else{
                buttonView.detailLabel.text=[NSString stringWithFormat:@"售价:%.2f元",[dic[@"salesPrice"] doubleValue]];
                buttonView.tagImageView.hidden=YES;
            }
            if ([dic[@"stock"] intValue]>0) {
                [buttonView setIsCanSelected:YES];
            }else{
                [buttonView setIsCanSelected:NO];
            }
        }
        [self.ui_borderView addSubview:buttonView];
    }
    self.ui_borderView.tag=9999;
}

- (void)setButtonSelectedWithTag:(NSInteger)tag
{
    for (XHButtonView *buttonView in self.ui_borderView.subviews) {
        [buttonView setSelected:NO];
    }
    XHButtonView *buttonView=[self.ui_borderView viewWithTag:tag];
    [buttonView setSelected:YES];
}

- (IBAction)helpButtonAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(helpButtonAction:)]) {
        [self.delegate helpButtonAction:sender];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
}

#pragma mark - XHButtonViewDelegate
- (void)buttonViewDidSelectedAction:(XHButtonView *)sender{
    if (sender.isCanSelected) {
        for (XHButtonView * buttonView in self.ui_borderView.subviews) {
            [buttonView setSelected:NO];
        }
        [sender setSelected:YES];
    }
    if ([self.delegate respondsToSelector:@selector(cellDidSelectedData:index:)]) {
        NSDictionary *dic=self.m_dataArray[sender.tag];
        [self.delegate cellDidSelectedData:dic index:sender.tag];
    }
}

@end
